package com.winskysoft.frame;

import java.sql.SQLException;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Label;

import com.winskysoft.database.DatabaseManager;

public class SqlInfoPanel extends Composite implements SelectionListener{
	private Text text;
	private Text urlTxt;
	private Text userTxt;
	private Text passTxt;
	private Label label;
	private Label label_1;
	private Button nextBon;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public SqlInfoPanel(Composite parent, int style) {
		super(parent, style);
		
		urlTxt = new Text(this, SWT.BORDER);
		urlTxt.setText("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8");
		urlTxt.setFont(SWTResourceManager.getFont("宋体", 10, SWT.NORMAL));
		urlTxt.setBounds(96, 52, 444, 25);
		
		Label urlLab = new Label(this, SWT.NONE);
		urlLab.setAlignment(SWT.CENTER);
		urlLab.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		urlLab.setBounds(10, 57, 80, 15);
		urlLab.setText("\u6570\u636E\u5E93\u5730\u5740");
		
		Button testBon = new Button(this, SWT.NONE);
		testBon.addSelectionListener(new TestBonSelectionListener());
		testBon.setBounds(317, 300, 85, 38);
		testBon.setText("\u6D4B\u8BD5\u8FDE\u63A5");
		
		nextBon = new Button(this, SWT.NONE);
		nextBon.setEnabled(false);
		nextBon.addSelectionListener(this);
		nextBon.setText("\u4E0B\u4E00\u6B65");
		nextBon.setBounds(425, 300, 85, 38);
		
		userTxt = new Text(this, SWT.BORDER);
		userTxt.setText("root");
		userTxt.setFont(SWTResourceManager.getFont("宋体", 10, SWT.NORMAL));
		userTxt.setBounds(96, 95, 157, 25);
		
		passTxt = new Text(this, SWT.PASSWORD);
		passTxt.setText("root");
		passTxt.setFont(SWTResourceManager.getFont("宋体", 10, SWT.NORMAL));
		passTxt.setBounds(96, 140, 157, 25);
		
		label = new Label(this, SWT.NONE);
		label.setText("\u7528\u6237\u540D");
		label.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		label.setAlignment(SWT.CENTER);
		label.setBounds(10, 100, 80, 15);
		
		label_1 = new Label(this, SWT.NONE);
		label_1.setText("\u5BC6\u7801");
		label_1.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NORMAL));
		label_1.setAlignment(SWT.CENTER);
		label_1.setBounds(10, 145, 80, 15);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
	@Override
	public void widgetSelected(SelectionEvent e) {
		TablePanel tablePanel=new TablePanel(getParent(), SWT.NONE);
		tablePanel.setSize(getSize());
		
		this.dispose();
		
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent e) {
		// TODO Auto-generated method stub
		
	}
	private class TestBonSelectionListener extends SelectionAdapter {
		@Override
		public void widgetSelected(SelectionEvent e) {
//			String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
//			String username="root";
//			String password="root";
			String url=urlTxt.getText();
			String username=userTxt.getText();
			String password=passTxt.getText();
			
			try {
				DatabaseManager databaseManager=DatabaseManager.getDatabaseManager(url, username, password);
				MessageDialog.openInformation((Shell) getParent(),"提示","连接成功");
				nextBon.setEnabled(true);
			} catch (Exception  e1) {
				MessageDialog.openError((Shell) getParent(),"提示","连接失败");
				//e1.printStackTrace();
			}
		}
	}
}
